<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of register
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Member extends CI_Controller {

    function member() {
        parent :: __construct();
        $this->load->library('form_validation');
        $this->load->model('location_model');
        $this->load->model('member_model');
    }

    function new_member($type) {
        $data = $this->set_common_data();
        $data['type'] = $type;
        $this->load->view('register', $data);
    }

    function set_common_data() {
        $data['know'] = $this->commoncall->get_pickup(20);
        $data['agree'] = $this->commoncall->get_pickup(21);
        $data['state'] = $this->location_model->get_states(356);
        $data['dealer_state'] = $this->location_model->get_states(356);
        return $data;
    }

    function new_member_submit($type) {
        /* IF MEMBER TYPE IS INDIVISUAL */
        if ($type == 1) {
            $this->form_validation->set_rules('ind_fname', 'First Name', 'trim|required|alpha|min_length[3]');
            $this->form_validation->set_rules('ind_lname', 'Last Name', 'trim|required|alpha|min_length[2]');
            $this->form_validation->set_rules('ind_email', 'Email', 'trim|required|valid_email|is_unique[member_master.email]');
            $this->form_validation->set_rules('ind_cemail', 'Confirm Email', 'trim|required|valid_email|matches[ind_email]');

            $this->form_validation->set_rules('ind_areacode', 'Area Code', 'trim|required|numeric');
            $this->form_validation->set_rules('ind_landline', 'Landline No', 'trim|required|numeric');
            $this->form_validation->set_rules('ind_mobile', 'Mobile No', 'trim|required|numeric|min_length[10]|is_unique[member_master.mobile]');

            $this->form_validation->set_rules('ind_state', 'State', 'trim|required|numeric');
            $this->form_validation->set_rules('ind_city', 'City', 'trim|required|numeric');
            $this->form_validation->set_rules('ind_agree', 'Agrrement', 'trim|required');

            if ($this->form_validation->run() == FALSE) {
                $data = $this->set_common_data();
                $data['type'] = 1;
                $this->load->view('register', $data);
            } else {

                $data = $this->set_common_data();
                $post_data = array(
                    'type' => 1,
                    'first_name' => $_POST['ind_fname'],
                    'last_name' => $_POST['ind_lname'],
                    'email' => $_POST['ind_email'],
                    'area_code' => $_POST['ind_areacode'],
                    'landline' => $_POST['ind_landline'],
                    'mobile' => $_POST['ind_mobile'],
                    'state' => $_POST['ind_state'],
                    'city' => $_POST['ind_city'],
                    'agree' => $_POST['ind_agree'],
                    'password' => $this->commoncall->random_text(6),
                    'ip' => $this->commoncall->get_ip(),
                    'status' => 51,
                    'created_on' => date('Y-m-d H:m:s', now())
                );

                $msg = $this->member_model->append_member($post_data);
                $this->session->set_flashdata('message', $msg);
                redirect('member/new_member_submit/2');
            }
        }

        /* IF MEMBER TYPE IS AGENT OR DEALER */
        if ($type == 2) {
            $this->form_validation->set_rules('del_comp', 'Business or Company Name', 'trim|required|alpha|min_length[3]');
            $this->form_validation->set_rules('del_fname', 'First Name', 'trim|required|alpha|min_length[3]');
            $this->form_validation->set_rules('del_lname', 'Last Name', 'trim|required|alpha|min_length[2]');
            $this->form_validation->set_rules('del_email', 'Email', 'trim|required|valid_email|is_unique[member_master.email]');
            $this->form_validation->set_rules('del_cemail', 'Confirm Email', 'trim|required|valid_email|matches[del_email]');
            $this->form_validation->set_rules('del_areacode', 'Area Code', 'trim|required|numeric');
            $this->form_validation->set_rules('del_landline', 'Landline No', 'trim|required|numeric');
            $this->form_validation->set_rules('del_mobile', 'Mobile No', 'trim|required|numeric|min_length[10]|is_unique[member_master.mobile]');
            $this->form_validation->set_rules('del_state', 'State', 'trim|required|numeric');
            $this->form_validation->set_rules('del_city', 'City', 'trim|required|numeric');
            $this->form_validation->set_rules('del_agree', 'Agrrement', 'trim|required');

            if ($this->form_validation->run() == FALSE) {
                $data = $this->set_common_data();
                $data['type'] = 2;
                $this->load->view('register', $data);
            } else {

                $data = $this->set_common_data();
                $post_data = array(
                    'type' => 2,
                    'company_name' => $_POST['del_comp'],
                    'first_name' => $_POST['del_fname'],
                    'last_name' => $_POST['del_lname'],
                    'email' => $_POST['del_email'],
                    'area_code' => $_POST['del_areacode'],
                    'landline' => $_POST['del_landline'],
                    'mobile' => $_POST['del_mobile'],
                    'state' => $_POST['del_state'],
                    'city' => $_POST['del_city'],
                    'agree' => $_POST['del_agree'],
                    'password' => $this->commoncall->random_text(6),
                    'ip' => $this->commoncall->get_ip(),
                    'status' => 51,
                    'created_on' => date('Y-m-d H:m:s', now())
                );

                $msg = $this->member_model->append_member($post_data);
                $this->session->set_flashdata('message', $msg);
                redirect('member/new_member/2');
            }
        }
    }
//-- MEMBER ACCOUNT --//
    function myaccount(){
        $this->member_model->check_logedin();
        $this->load->view('myaccount');
    }
//---LOGIN---------------/    
    function login($type) {
        $data['type'] = $type;
        $this->load->view('login', $data);
    }
//---LOGIN SUBMIT ---//
    function login_submit() {
        $this->form_validation->set_rules('login_email', 'Login Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('login_password', 'Password', 'trim|required|min_length[4]');
        if ($this->form_validation->run() == FALSE) {
            $data['type'] = 1;
            $this->load->view('login', $data);
        } else {
            $post_data = array(
                'email' => $_POST['login_email'],
                'password' => $_POST['login_password']);
            $msg = $this->member_model->check_member($post_data);
            if ($msg) {
                $data['type'] = 1;
                $this->session->set_flashdata('message', $msg);
                $this->load->view('login', $data);
            }else{
                redirect('member/myaccount');
            }
        }
    }
//--MEMBER LOGOUT ---//    
    function logout(){
        if($this->session->userdata('mem_email')){
            $this->session->unset_userdata('mem_email');
            redirect('member/login/1');
        }
    }
//--    
    
}

?>
